package session.stateless;

import entity.Employee;
import entity.Message;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.Calendar;

@Stateful
public class MessageSessionBean implements MessageSessionBeanLocal {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public List<Message> getMessages(String userId, String type) {
        ArrayList al = new ArrayList();

        if (type.equals("sent")) { //get sent messages
            Query query = entityManager.createQuery("SELECT m FROM Message m WHERE m.senderId = :insenderId ORDER BY m.messageDate DESC");
            query.setParameter("insenderId", userId);

            for (Object o : query.getResultList()) {
                Message message = (Message) o;

                if (!message.getOwner().getUserId().equals(userId)) {
                    al.add(message);
                }
            }

            return al;
        } else { //get received messages
            Query query = entityManager.createQuery("SELECT m FROM Message m WHERE m.receiverId = :inreceiverId ORDER BY m.messageDate DESC");
            query.setParameter("inreceiverId", userId);

            for (Object o : query.getResultList()) {
                Message message = (Message) o;

                if (message.getOwner().getUserId().equals(userId)) {
                    al.add(message);
                }
            }

            return al;
        }
    }

    @Override
    public Message getMessage(String messageId) {
        Query query = entityManager.createQuery("SELECT m FROM Message m WHERE m.messageId = :inmessageId");
        query.setParameter("inmessageId", messageId);

        Message message = null;

        try {
            message = (Message) query.getSingleResult();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return message;
    }

    @Override
    public void addMessage(String senderId, String receiverId, String subject, String content, Employee owner) {
        Calendar cal = Calendar.getInstance();
        Timestamp ts = new Timestamp(cal.getTime().getTime());
        
        Employee temp = entityManager.find(Employee.class, owner.getUserId());

        Message message = new Message(senderId, receiverId, subject, content, ts, temp);
        
        entityManager.persist(message);
        entityManager.flush();
    }

    @Override
    public void deleteMessage(Long messageId) {
        Message message = entityManager.find(Message.class, messageId);

        if (message != null) {
            System.out.println(messageId + " is deleted.");
            entityManager.remove(message);
        }
    }

    @Override
    public void updateStatus(Long messageId) {
        Message message = entityManager.find(Message.class, messageId);

        if (message != null) {
            System.out.println(messageId + " status is updated.");
            message.setMessageStatus(true);
            entityManager.persist(message);
            entityManager.flush();
        }
    }
}
